<?php
namespace Home\Controller;

use Think\Controller;

class BaseController extends Controller
{


    // 404 错误定向
    protected function _404($message = '', $jumpUrl = '', $waitSecond = 3)
    {
        $this->assign('message', '访问的页面不存在！');
        if (!empty($jumpUrl)) {
            $this->assign('jumpUrl', $jumpUrl);
            $this->assign('waitSecond', $waitSecond);
        }
        $this->display(C('TMPL_ACTION_ERROR'));
    }

    function insert()
    {
        $model = D($this->getActionName());
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        //保存当前数据对象
        if ($result = $model->add()) { //保存成功
            // 回调接口
            if (method_exists($this, '_tigger_insert')) {
                $model->id = $result;
                $this->_tigger_insert($model);
            }
            //成功提示
            $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
            $this->success(L('新增成功'));
        } else {
            //失败提示
            $this->error(L('新增失败'));
        }
    }

    public function add()
    {
        $this->display();
    }

    function read()
    {
        $this->edit();
    }


    function update()
    {
        $model = D($this->getActionName());
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        // 更新数据
        if (false !== $model->save()) {
            // 回调接口
            if (method_exists($this, '_tigger_update')) {
                $this->_tigger_update($model);
            }
            //成功提示
            $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
            $this->success(L('更新成功'));
        } else {
            //错误提示
            $this->error(L('更新失败'));
        }
    }

    /**
     * +----------------------------------------------------------
     * 取得操作成功后要返回的URL地址
     * 默认返回当前模块的默认操作
     * 可以在controller控制器中重载
     * +----------------------------------------------------------
     * @access public
     * +----------------------------------------------------------
     * @return string
    +----------------------------------------------------------
     * @throws ThinkExecption
    +----------------------------------------------------------
     */
    function getReturnUrl()
    {
        return __URL__ . '?' . C('VAR_MODULE') . '=' . MODULE_NAME . '&' . C('VAR_ACTION') . '=' . C('DEFAULT_ACTION');
    }

    function set_pagination($count, $offset = 1, $rollpage = 5)
    {
        $Page = new  \Think\Page($count, $offset);// 实例化分页类 传入总记录数
        $Page->rollPage = $rollpage;
        $Page->setConfig('header', '<span><span>共%TOTAL_PAGE%页%TOTAL_ROW% 条记录<span></span>');
        $Page->setConfig('prev', '<span>〈〈</span>');
        $Page->setConfig('next', '<span>〉〉</span>');
        $Page->setConfig('first', '<span>首页');
        $Page->setConfig('last', '末页');
        // $Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $firstRow = $Page->firstRow;
        $listRows = $Page->listRows;
        $data = array(
            'firstRow' => $firstRow,
            'listRows' => $listRows,
            'page_str' => $Page->show(),
        );
        //$Page->setConfig('theme','<span>共 %TOTAL_ROW% 条记录</span>');
        return $data;// 分页显示输出
    }
}

?>